package cn.jiahua.hospital.service.impl;


import cn.jiahua.hospital.config.AliPayConfig;
import cn.jiahua.hospital.pojo.YOrder;
import cn.jiahua.hospital.service.AlipayService;
import cn.jiahua.hospital.service.YOrderService;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AlipayServiceImpl implements AlipayService {

    @Resource
    AliPayConfig aliPayConfig;

    @Resource
    YOrderService yOrderService;

    @Override
    public boolean alipayRefunt(Map<String,String> map, HttpServletResponse response) throws Exception {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig.getUrl(), aliPayConfig.getAppid(), aliPayConfig.getRsaPrivateKey(), "json", aliPayConfig.getCharset(), aliPayConfig.getAlipayPublicKey(), aliPayConfig.getSignType());
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        //修改数据库----设置取消订单状态
        String orderId = map.get("WIDTRout_trade_no");

        //根据订单编号获取订单信息
        Map<String, Object> map02 = new HashMap<>();
        map02.put("orderid", orderId);
        List<YOrder> yOrderListByMapOfUser = yOrderService.getYOrderListByMapOfUser(map02);
        if (yOrderListByMapOfUser == null || yOrderListByMapOfUser.size() <= 0) {
            return false;
        }

        //修改点订单的状态
        YOrder yOrder = yOrderListByMapOfUser.get(0);
        yOrder.setStatus(4);

        Integer integer = yOrderService.qdtxModifyYOrder(yOrder);

        if (integer <= 0) {
            return false;
        }

        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = new String(map.get("WIDTRout_trade_no").getBytes("ISO-8859-1"), "UTF-8");
        //支付宝交易号
        //String trade_no = new String(refuntDto.getWIDTRtrade_no().getBytes("ISO-8859-1"), "UTF-8");
        //请二选一设置
        //需要退款的金额，该金额不能大于订单金额，必填
        String refund_amount = new String(map.get("WIDTRrefund_amount").getBytes("ISO-8859-1"), "UTF-8");
        //退款的原因说明
        String refund_reason = new String(map.get("WIDTRrefund_reason").getBytes("ISO-8859-1"), "UTF-8");
        //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
        String out_request_no = new String(map.get("WIDTRout_request_no").getBytes("ISO-8859-1"), "UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"trade_no\":\"" + "" + "\","
                + "\"refund_amount\":\"" + refund_amount + "\","
                + "\"refund_reason\":\"" + refund_reason + "\","
                + "\"out_request_no\":\"" + out_request_no + "\"}");

        //请求
        String result = null;

        result = alipayClient.execute(alipayRequest).getBody();

        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONObject jsonObject02 = (JSONObject) jsonObject.get("alipay_trade_refund_response");
        //输出
        System.out.println(jsonObject02.get("code"));
        String code = (String) jsonObject02.get("code");
        if (code!=null && code.equals("10000")){
            System.out.println("***********退款成功***********");
            return true;
        }else{
            System.out.println("***********退款失败***********");
            return false;
        }
    }
}
